package cc.mrbird.febs.power.controller;


import cc.mrbird.febs.common.utils.MD5Util;
import cc.mrbird.febs.common.utils.R;
import cc.mrbird.febs.power.entity.*;
import cc.mrbird.febs.power.service.*;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author MrBird
 */
@RestController
@RequestMapping("/power/account-info")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class AccountInfoController {

    private SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    private final IAccountInfoService iAccountInfoService;

    private final IAccountBindAppInfoService iAccountBindAppInfoService;

    private final IAccountBindRoleInfoService iAccountBindRoleInfoService;

    private final IAppInfoService iAppInfoService;

    private final IMenuInfoService iMenuInfoService;

    private final IRoleInfoService iRoleInfoService;


    /**
     * 分页获取账户信息
     *
     * @param page
     * @param accountInfo
     * @return
     */
    @GetMapping
    public R getAccountInfoByPage(Page page, AccountInfo accountInfo) {
        return R.ok(iAccountInfoService.getAccountInfoByPage(page, accountInfo));
    }

    /**
     * all
     *
     * @return
     */
    @GetMapping("/list")
    public R getAccountInfoByList() {
        return R.ok(iAccountInfoService.list());
    }

    /**
     * 新增授权账户
     *
     * @param accountInfo
     * @return
     */
    @PostMapping("/AccountInfoAdd")
    public R AccountInfoAdd(AccountInfo accountInfo) {
        Date d = new Date();
        accountInfo.setCreateDate(sdf3.format(d));
        accountInfo.setAccountPwd(MD5Util.encrypt(accountInfo.getAccountName(), accountInfo.getAccountPwd()));
        accountInfo.setExpireDate(sdf3.format(new Date(d.getTime() + (long) 365 * 24 * 60 * 60 * 1000)));
        iAccountInfoService.save(accountInfo);
        return R.ok(accountInfo.getAccountCode());
    }

    /**
     * 绑定
     *
     * @param appId
     * @param roleId
     * @param accountCode
     */
    @PostMapping("/bindAppAndRole")
    public void bindAppAndRole(String appId, String roleId, String accountCode) {
        AccountInfo account = iAccountInfoService.getOne(Wrappers.<AccountInfo>lambdaQuery().eq(AccountInfo::getAccountCode, accountCode));
        System.out.println(account);
        if (account != null) {
            if (!("").equals(appId) && appId != null) {
                iAccountBindAppInfoService.save(new AccountBindAppInfo(account.getAccountId(), appId));
            }
            if (!("").equals(roleId) && roleId != null) {
                iAccountBindRoleInfoService.save(new AccountBindRoleInfo(account.getAccountId(), roleId));
            }
        }
    }

    /**
     * 根据账户id获取app与角色信息
     *
     * @param id
     * @return
     */
    @GetMapping("/getMessageByAccount/{id}")
    public R getMessageByAccount(@PathVariable("id") Integer id) {
        AccountBindRoleInfo accountBindRoleInfo = iAccountBindRoleInfoService.getOne(Wrappers.<AccountBindRoleInfo>lambdaQuery().eq(AccountBindRoleInfo::getAccountId, id));
        AccountBindAppInfo accountBindAppInfo = iAccountBindAppInfoService.getOne(Wrappers.<AccountBindAppInfo>lambdaQuery().eq(AccountBindAppInfo::getAccountId, id));
        Map map = new HashMap();
        if (accountBindAppInfo != null) {
            map.put("accountBindAppInfo", accountBindAppInfo.getAppId().split(","));
        }
        if (accountBindRoleInfo != null) {
            map.put("accountBindRoleInfo", accountBindRoleInfo.getRoleId().split(","));
        }
        return R.ok(map);
    }

    /**
     * 修改授权账户
     *
     * @param accountInfo
     * @return
     */
    @PutMapping("/accountInfoEdit")
    public R AccountInfoEdit(AccountInfo accountInfo, String appId, String roleId) {
        System.out.println("====" + accountInfo.toString());
        iAccountBindRoleInfoService.update(Wrappers.<AccountBindRoleInfo>lambdaUpdate().set(AccountBindRoleInfo::getRoleId, roleId)
                .eq(AccountBindRoleInfo::getAccountId, accountInfo.getAccountId()));
        iAccountBindAppInfoService.update(Wrappers.<AccountBindAppInfo>lambdaUpdate().set(AccountBindAppInfo::getAppId, appId)
                .eq(AccountBindAppInfo::getAccountId, accountInfo.getAccountId()));
        return R.ok(iAccountInfoService.updateById(accountInfo));
    }

    /**
     * 根据ID删除记录
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public R AccountInfoDel(@PathVariable Integer id) {
        return R.ok(iAccountInfoService.removeById(id));
    }

    /**
     * 批量删除账户
     *
     * @param ids
     * @return
     */
    @DeleteMapping("/man/{ids}")
    public R AccountInfosDel(@PathVariable("ids") List<Integer> ids) {
        return R.ok(iAccountInfoService.removeByIds(ids));
    }

    @GetMapping("/sideLogin")
    public R sideLogin(String name, String pwd) {
        Map result = new HashMap();
        if (name != null && pwd != null) {
            String loginToken = MD5Util.encrypt(name, pwd);
            AccountInfo accountInfo = iAccountInfoService.getOne(Wrappers.<AccountInfo>lambdaQuery().eq(loginToken != null, AccountInfo::getAccountPwd, loginToken));
            if (accountInfo != null) {
                AccountBindAppInfo accountBindAppInfo = iAccountBindAppInfoService.getOne(Wrappers.<AccountBindAppInfo>lambdaQuery().eq(accountInfo.getAccountId() != null, AccountBindAppInfo::getAccountId, accountInfo.getAccountId()));
                List<AppInfo> appInfos = iAppInfoService.list(Wrappers.<AppInfo>lambdaQuery().in(AppInfo::getAppId, accountBindAppInfo.getAppId().split(",")));
                result.put("appInfos", appInfos);

                AccountBindRoleInfo accountBindRoleInfo = iAccountBindRoleInfoService.getOne(Wrappers.<AccountBindRoleInfo>lambdaQuery().eq(accountInfo.getAccountId() != null,AccountBindRoleInfo::getAccountId,accountInfo.getAccountId()));
                List<RoleInfo> roleInfos = iRoleInfoService.list(Wrappers.<RoleInfo>lambdaQuery().in(RoleInfo::getRoleId,accountBindRoleInfo.getRoleId().split(",")));
                result.put("roleInfos", roleInfos);
            }
        }

        return R.ok(result);
    }
}
